package com.example.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.myapplication.R;
import com.example.myapplication.bean.User;
import com.example.myapplication.utils.IP;
import com.example.myapplication.utils.OkHttpUtil;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private EditText etName;
    private EditText etPhone;
    private EditText etPassword;
    private EditText etRePassword;
    private Toolbar toolbar;

    private String name;
    private String phone;
    private String password;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        //test();
    }

    private void test() {
        User user = new User(1,"djp","336648","1234567");
        gson = new Gson();
        String s = gson.toJson(user);
        Log.d(TAG, "test: " + s);
    }

    private void init() {
        etName = findViewById(R.id.et_name_register);
        etPhone = findViewById(R.id.et_phone_register);
        etPassword = findViewById(R.id.et_password_register);
        etRePassword = findViewById(R.id.et_repassword_register);
        toolbar = findViewById(R.id.tb_register);
        gson = new Gson();

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    public void toLogin(View view) {
        startActivity(new Intent(this, LoginActivity.class));
    }

    public void register(View view) {
        if (canRegister()) {
            new Thread(){
                @Override
                public void run() {
                    User user = new User(name, password, phone);
                    try {
                        sendRegisterReq(user);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }

    private boolean canRegister() {
        String username = etName.getText().toString();
        String userphone = etPhone.getText().toString();
        String userpassword = etPassword.getText().toString();
        String userrepassword = etRePassword.getText().toString();

        if (!username.isEmpty()) {
            if (!userphone.isEmpty()) {
                if (!userpassword.isEmpty()) {
                    if (!userrepassword.isEmpty()) {
                        if (userpassword.equals(userrepassword)) {
                            name = username;
                            phone = userphone;
                            password = userpassword;
                            return true;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public void sendRegisterReq(User user) throws JSONException {
        JSONObject post = OkHttpUtil.post(user, "/user/register");
        if (post != null) {
            final User user1 = gson.fromJson(post.toString(), User.class);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    user1.toastMassage(RegisterActivity.this);
                }
            });
            if (user1.isSuccess()) {
                startActivity(new Intent(this, LoginActivity.class));
                finish();
            }
        }
    }
}